#!/bin/sh
# Copyright (C) 2022 jjm2473

if [ ! -f /etc/config/fstab ]; then
    /usr/libexec/blockmount.sh detect 2>/dev/null
fi

uci -q batch <<-EOF >/dev/null
    set system.@system[-1].zonename='Asia/Shanghai'
    set system.@system[-1].timezone='CST-8'
    commit system
EOF

uci set dockerd.globals.data_root=/overlay/upper/opt/docker

if [ ! -f /proc/device-tree/chosen/default-firewall ] || grep -qFw nas /proc/device-tree/chosen/default-firewall; then
    uci -q batch <<-EOF >/dev/null
        delete ucitrack.@wan_drop[-1]
        add ucitrack wan_drop
        set ucitrack.@wan_drop[-1].init=wan_drop
        commit ucitrack
EOF

    WAN_ZONE=`uci show firewall | egrep '^firewall\.@zone\[\d+\]\.name=' | grep wan | head -n1 | head -c -12`
    uci -q batch <<-EOF >/dev/null
        del_list dockerd.firewall.blocked_interfaces=wan
        set ${WAN_ZONE}.input=ACCEPT
        commit firewall
EOF
    if [ -e /etc/config/minidlna ]; then
        uci -q batch <<-EOF >/dev/null
            set minidlna.@minidlna[0].interface='br-lan,eth0'
            commit minidlna
EOF
    fi
    if [ -e /etc/config/ttyd ]; then
        uci -q batch <<-EOF >/dev/null
            delete ttyd.@ttyd[0].interface
            commit ttyd
EOF
    fi
    for samba in samba4 samba ; do
        if [ -e /etc/config/$samba ]; then
            uci -q batch <<-EOF >/dev/null
                set $samba.@samba[0].interface='lan wan'
                commit $samba
EOF
        fi
    done
fi

uci -q batch <<-EOF >/dev/null
    commit dockerd
EOF

uci -q batch <<-EOF >/dev/null
    set upnpd.config.ipv6_disable=1
    commit upnpd
EOF

uci -q batch <<-EOF >/dev/null
    set luci.apply.rollback=30
    set luci.apply.holdoff=2
    commit luci
EOF

if [ -f /etc/config/argon ]; then
    uci -q batch <<-EOF >/dev/null
        set argon.@global[0].mode=light
        commit argon
EOF
fi

exit 0
